/**
 * Created by frank on 16-1-20.
 */
$(function(){
    $('#dg').datagrid({
        method: 'get',
        url: 'find_operator_list.json'
    });
})

var url;
function newUser() {
    $('#dlg').dialog({
        title: '新增用户',
        modal: true
    });
    $('#dlg').dialog('open').dialog('center');
    $('#fm').form('clear');
    $("#operatorState").combobox('select', 'NORMAL');
    url = 'add_operator.json';
}
function editUser() {
    var rows = $('#dg').datagrid('getSelections');
    if (rows && rows.length == 1) {
        $('#fm').form('clear');
        $("#operatorId").val(rows[0].operatorId);
        $.get("get_operator_info.json?id=" + rows[0].operatorId, function (data) {
            $('#dlg').dialog('open').dialog('center').dialog('setTitle', '编辑用户');
            $('#fm').form('load', data);
            if (!data.organizationId) {
                $('#organizationId').combotree('setValue', '');
            }
            $.get("get_operator_roleIds.json?id=" + rows[0].operatorId, function (data) {
                $('#roles').combobox('setValues', data.split(','))
            });
        });
        url = 'update_operator.json';
    } else {
        EGIFT_APP.showEditWarnFunc();
    }
}
function saveUser() {
    $('#fm').form('submit', {
        url: url,
        onSubmit: function () {
            var result = $(this).form('validate');
            if(result) {
                $('#dlg').dialog('close');
            }
            return result;
        },
        success: function (result) {
            if (result == 'success') {
                $.messager.show({
                    title: '成功',
                    msg: "新增、修改用户成功"
                });
            } else {
                $.messager.show({
                    title: '失败',
                    msg: "新增、修改用户失败"
                });
            }
            $('#dg').datagrid('reload');    // reload the user data
        }
    });
}
function deleteUser() {
    var rows = $('#dg').datagrid('getSelections');
    if (rows && rows.length > 0) {
        $.messager.confirm('确认', '确认要删除吗？', function (r) {
            if (r) {
                var ids = EGIFT_APP.convertIds2Str(rows,"operatorId");
                $.post('delete_operator.json', {ids: ids}, function (result) {
                    if (result == "success") {
                        $.messager.show({
                            title: '成功',
                            msg: "删除用户成功"
                        });
                        $('#dg').datagrid('reload');    // reload the user data
                    } else {
                        $.messager.show({
                            title: '失败',
                            msg: "删除用户失败"
                        });
                    }
                });
            }
        });
    } else {
        EGIFT_APP.showDeleteWarnFunc();
    }
}

function searchUser() {
    $('#dg').datagrid('load', {
        operatorName: $('#operatorName1').val(),
        loginName: $('#loginName1').val()
    });
}